package com.wooboo.server.ext.action.redis;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import com.wooboo.server.ext.ActionBase;
import com.wooboo.server.ext.ApiError;
import com.wooboo.server.ext.Result;
import com.wooboo.server.ext.data.Idea;
import com.wooboo.server.ext.data.RestModel;
import com.wooboo.server.ext.data.SysLogger;
import com.wooboo.server.ext.data.Traffic;
import com.wooboo.server.ext.data.UserFinance;
import com.wooboo.server.ext.dbutil.JdbcTemplate;
import com.wooboo.server.ext.dbutil.Sql;
import com.wooboo.server.ext.service.DbService;
import com.wooboo.server.ext.service.PushService;
import com.wooboo.server.ext.util.DateUtil;
import com.wooboo.server.ext.util.HttpClientUtils;
import com.wooboo.server.ext.util.NumberUtil;
import com.wooboo.server.ext.util.Parameters;
import com.wooboo.server.ext.util.StringUtil;

/**
 * 
 * 
 * @author duanwei
 * 
 */
public class Push extends ActionBase {

	public String id;// 计划id、组id、创意id
	public String action; // 动作标识，plan_open/plan_close/group_open/group_close/idea_open/idea_close

	public Push() {
		needCheckAuth = false;
	}

	@Override
	public void initParameters(Parameters params) throws ApiError {
		id = params.getFieldValue("id");
		action = params.getFieldValue("action");
		if(StringUtil.isEmpty(id,action)){
		    throw ApiError.Type.INVALID_PARAM.toException();
		}
	}

	@Override
	public Result invokeService() throws ApiError {
		Result result = new Result();
		
		RestModel restModel = new RestModel();
		
		
		//本次操作成功的创意id
		List<Long> succ = new ArrayList<Long>();
		
		
		PushService pushService = new PushService();

		JdbcTemplate jdbcTemplate = new JdbcTemplate();
		
		if ("plan_open".equals(action)) {
			

			// 开启计划中所有符合的创意：1先把计划状态改为开启，2然后查询所有符合条件的创意加入缓存

			jdbcTemplate.updateObject(new Sql(
					" update admanage_plan set is_close = 1 where id = " + id));

			/**
			 * select a.id from admanage_idea a,admanage_group b,admanage_plan c
			 * where a.group_id = b.id and a.plan_id = c.id and a.is_valid = 1
			 * and b.is_valid = 1 and c.is_valid = 1 and a.is_close = 1 and
			 * b.is_close = 1 and c.is_close = 1 and a.plan_id =
			 */

			Sql sql = new Sql(
					" select a.id from admanage_idea a,admanage_group b,admanage_plan c ");
			sql.append(" where a.group_id = b.id and a.plan_id = c.id ");
			sql.append(" and a.is_valid = 1 and b.is_valid = 1 and c.is_valid = 1 ");
			sql.append(" and a.is_close = 1 and b.is_close = 1 and c.is_close = 1 ");
			sql.append(" and a.plan_id = " + id);
			List<Idea> ideaList = jdbcTemplate.queryForList(sql, Idea.class);
			if (ideaList != null && ideaList.size() > 0) {
				for (Idea idea : ideaList) {
					boolean b = pushService.putIdeaToReadis(idea.getId());
					if (b) {
						succ.add(idea.getId());
					}
				}

			}

		} else if ("plan_close".equals(action)) {


			// 关闭计划中所有符合的创意：1查询所有符合条件的创意移除缓存,2.计划状态改为关闭，

			/**
			 * select a.id from admanage_idea a,admanage_group b,admanage_plan c
			 * where a.group_id = b.id and a.plan_id = c.id and a.is_valid = 1
			 * and b.is_valid = 1 and c.is_valid = 1 and a.is_close = 1 and
			 * b.is_close = 1 and c.is_close = 1 and a.plan_id =
			 */

			Sql sql = new Sql(
					" select a.id from admanage_idea a,admanage_group b,admanage_plan c ");
			sql.append(" where a.group_id = b.id and a.plan_id = c.id ");
			sql.append(" and a.is_valid = 1 and b.is_valid = 1 and c.is_valid = 1 ");
			sql.append(" and a.is_close = 1 and b.is_close = 1 and c.is_close = 1 ");
			sql.append(" and a.plan_id = " + id);
			List<Idea> ideaList = jdbcTemplate.queryForList(sql, Idea.class);
			if (ideaList != null && ideaList.size() > 0) {
				for (Idea idea : ideaList) {
					boolean b = pushService.delIdeaFromReadis(idea.getId());
					if (b) {
						succ.add(idea.getId());
					}
				}

			}

			jdbcTemplate.updateObject(new Sql(
					" update admanage_plan set is_close = 0 where id = " + id));
		} else if ("group_open".equals(action)) {


			// 开启组中所有符合的创意：1.组状态改为开启，2查询所有符合条件的创意加入缓存
			jdbcTemplate.updateObject(new Sql(
					" update admanage_group set is_close = 1 where id = " + id));
			
			/**
			 * select a.id from admanage_idea a,admanage_group b,admanage_plan c
			 * where a.group_id = b.id and a.plan_id = c.id and a.is_valid = 1
			 * and b.is_valid = 1 and c.is_valid = 1 and a.is_close = 1 and
			 * b.is_close = 1 and c.is_close = 1 and a.group_id =
			 */

			Sql sql = new Sql(
					" select a.id from admanage_idea a,admanage_group b,admanage_plan c ");
			sql.append(" where a.group_id = b.id and a.plan_id = c.id ");
			sql.append(" and a.is_valid = 1 and b.is_valid = 1 and c.is_valid = 1 ");
			sql.append(" and a.is_close = 1 and b.is_close = 1 and c.is_close = 1 ");
			sql.append(" and a.group_id = " + id);
			List<Idea> ideaList = jdbcTemplate.queryForList(sql, Idea.class);
			if (ideaList != null && ideaList.size() > 0) {
				for (Idea idea : ideaList) {
					boolean b = pushService.putIdeaToReadis(idea.getId());
					if (b) {
						succ.add(idea.getId());
					}
				}

			}

		} else if ("group_close".equals(action)) {


			// 关闭组中所有符合的创意：1查询所有符合条件的创意移除缓存,2.组状态改为关闭，

			/**
			 * select a.id from admanage_idea a,admanage_group b,admanage_plan c
			 * where a.group_id = b.id and a.plan_id = c.id and a.is_valid = 1
			 * and b.is_valid = 1 and c.is_valid = 1 and a.is_close = 1 and
			 * b.is_close = 1 and c.is_close = 1 and a.group_id =
			 */

			Sql sql = new Sql(
					" select a.id from admanage_idea a,admanage_group b,admanage_plan c ");
			sql.append(" where a.group_id = b.id and a.plan_id = c.id ");
			sql.append(" and a.is_valid = 1 and b.is_valid = 1 and c.is_valid = 1 ");
			sql.append(" and a.is_close = 1 and b.is_close = 1 and c.is_close = 1 ");
			sql.append(" and a.group_id = " + id);
			List<Idea> ideaList = jdbcTemplate.queryForList(sql, Idea.class);
			if (ideaList != null && ideaList.size() > 0) {
				for (Idea idea : ideaList) {
					boolean b = pushService.delIdeaFromReadis(idea.getId());
					if (b) {
						succ.add(idea.getId());
					}
				}

			}

			jdbcTemplate.updateObject(new Sql(
					" update admanage_group set is_close = 0 where id = " + id));

		} else if ("idea_open".equals(action)) {

			
			//开启所有符合的创意：1.创意状态改为开启，2查询所有符合条件的创意加入缓存
			jdbcTemplate.updateObject(new Sql(" update admanage_idea set is_close = 1 where id = " + id));
			
			
			/**
			 * select a.id from admanage_idea a,admanage_group b,admanage_plan c
			 * where a.group_id = b.id and a.plan_id = c.id and a.is_valid = 1
			 * and b.is_valid = 1 and c.is_valid = 1 and a.is_close = 1 and
			 * b.is_close = 1 and c.is_close = 1 and a.id =
			 */

			Sql sql = new Sql(
					" select a.id from admanage_idea a,admanage_group b,admanage_plan c ");
			sql.append(" where a.group_id = b.id and a.plan_id = c.id ");
			sql.append(" and a.is_valid = 1 and b.is_valid = 1 and c.is_valid = 1 ");
			sql.append(" and a.is_close = 1 and b.is_close = 1 and c.is_close = 1 ");
			sql.append(" and a.id = " + id);
			List<Idea> ideaList = jdbcTemplate.queryForList(sql, Idea.class);
			if (ideaList != null && ideaList.size() > 0) {
				for (Idea idea : ideaList) {
					boolean b = pushService.putIdeaToReadis(idea.getId());
					if (b) {
						succ.add(idea.getId());
					}
				}

			}


		} else if ("idea_close".equals(action)) {

			// 关闭所有符合的创意：1查询所有符合条件的创意移除缓存,2.创意状态改为关闭，

			/**
			 * select a.id from admanage_idea a,admanage_group b,admanage_plan c
			 * where a.group_id = b.id and a.plan_id = c.id and a.is_valid = 1
			 * and b.is_valid = 1 and c.is_valid = 1 and a.is_close = 1 and
			 * b.is_close = 1 and c.is_close = 1 and a.id =
			 */

			Sql sql = new Sql(
					" select a.id from admanage_idea a,admanage_group b,admanage_plan c ");
			sql.append(" where a.group_id = b.id and a.plan_id = c.id ");
			sql.append(" and a.is_valid = 1 and b.is_valid = 1 and c.is_valid = 1 ");
			sql.append(" and a.is_close = 1 and b.is_close = 1 and c.is_close = 1 ");
			sql.append(" and a.id = " + id);
			List<Idea> ideaList = jdbcTemplate.queryForList(sql, Idea.class);
			if (ideaList != null && ideaList.size() > 0) {
				for (Idea idea : ideaList) {
					boolean b = pushService.delIdeaFromReadis(idea.getId());
					if (b) {
						succ.add(idea.getId());
					}
				}

			}

			jdbcTemplate.updateObject(new Sql(
					" update admanage_idea set is_close = 0 where id = " + id));

		}
		
		restModel.setCode(0);
		restModel.setErrorDescription("操作成功");
		restModel.setDataObject(succ);
		result.setRet(JSONObject.fromObject(restModel));
		return result;
		
	}

}
